$(function () {

    var content = '';

    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/admin/Upload/layuiUpload' //接口url
                ,type: 'post' //默认post
                ,done: function(res){
                    //上传完毕回调
                    if(res.status == 1){
                        //
                    }
                }
                ,error: function(){
                    //请求异常回调
                }
            }
        });

        content = layedit.build('content'); //建立编辑器
        layedit.setContent(content, $('#content').text());
    });

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value == ''){
                    return '标题不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            var param = data.field;
            var layedit = layui.layedit;
            param.details = layedit.getContent(content);

            //发异步，把数据提交给php
            $.ajax({
                url:save_url,
                data:param,
                type:"Post",
                dataType:"json",
                success:function(data){
                    if(data.status == 1){
                        layer.msg("添加成功");
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function(data){
                    layer.msg("服务器无响应");
                }
            });

            return false;
        });
    });

});